﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using Visifire.Charts;
using Visifire.Commons;

namespace GraphDrawer.Reports {
	public partial class BackgroundSimulationMonitor : UserControl {
		private GraphProvider.GraphProviderServiceSoapClient cl;
		ReportsWizard reports;
		DataSeries dataSeries;
		
		int maxSteps;
		bool done;
		bool init;

		public BackgroundSimulationMonitor(GraphProvider.GraphProviderServiceSoapClient cl, ReportsWizard reports) {
			InitializeComponent();
			this.cl = cl;
			this.reports = reports;

			chart.Width = LayoutRoot.Width;
			chart.Height = LayoutRoot.Height;

			dataSeries = new DataSeries();
            dataSeries.RenderAs = RenderAs.Bar;

			cl.getExporimentStateCompleted += new EventHandler<GraphDrawer.GraphProvider.getExporimentStateCompletedEventArgs>(cl_getExporimentStateCompleted);
			LayoutRoot.SizeChanged += new SizeChangedEventHandler(LayoutRoot_SizeChanged);
		}

		public void Init(int maxSteps) {
			this.maxSteps = maxSteps;
			init = true;
			chart.AxesY.Clear();
			chart.Series.Clear();
			dataSeries.DataPoints.Clear();
		}

		public void Start() {
			cl.getExporimentStateAsync();
		}

		void cl_getExporimentStateCompleted(object sender, GraphDrawer.GraphProvider.getExporimentStateCompletedEventArgs e) {
			var exStates = e.Result;
			done = true;

			if (init == true) {
				init = false;
				
				for (int i = 0; i < exStates.Count; ++i) {
					DataPoint dataPoint = new DataPoint();

					if (exStates[i].running) {
						done = false;
						dataPoint.Color = new SolidColorBrush(Colors.Yellow);
						dataPoint.AxisXLabel = "Running";
					}
					else {
						dataPoint.Color = new SolidColorBrush(Colors.Green);
						dataPoint.AxisXLabel = "Completed";
					}
					dataPoint.YValue = exStates[i].simTime;

					dataSeries.DataPoints.Add(dataPoint);
				}

				chart.AxesY.Add(new Axis() {
					Title = "Simulation time [steps]"
				});
				chart.Series.Add(dataSeries);
			}
			else
				for (int i = 0; i < exStates.Count; ++i) {
					if (exStates[i].running) {
						done = false;
						dataSeries.DataPoints[i].Color = new SolidColorBrush(Colors.Yellow);
						dataSeries.DataPoints[i].AxisXLabel = "Running";
					}
					else {
						dataSeries.DataPoints[i].Color = new SolidColorBrush(Colors.Green);
						dataSeries.DataPoints[i].AxisXLabel = "Completed";
					}
					dataSeries.DataPoints[i].YValue = exStates[i].simTime;
				}
			
			if (done == false)
				cl.getExporimentStateAsync();
			
			reports.EnableNext();
		}

		void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e) {
			if (chart != null) {
				chart.Width = e.NewSize.Width;
				chart.Height = e.NewSize.Height;
			}
		}
	}
}
